﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


namespace Nirvana
{
    [Serializable]
    public sealed class CheckAnimation
    {
        [Tooltip("The max bones for a SkinnedMeshRenderer.")]
        [SerializeField]
        private int maxBones = -1;

        [SerializeField]
        [Tooltip("The max curve count for animation clip.")]
        private int maxCurveCountForClip = -1;

        [SerializeField]
        [Tooltip("The max key frame count for animation clip.")]
        private int maxKeyFrameCountForClip = -1;


        public void CheckSkinMesh(AssetCheckReport report,SkinnedMeshRenderer skin)
        {
            if(this.maxBones >= 0)
            {
                var bones = skin.bones;
                if(bones.Length > this.maxBones)
                {
                    report.ShowWarnInfo("The SkinnedMeshRenderer <b><color=orange>{0}</color></b> has <b><color=orange>{1}</color></b> bones, more than {2}.",
                        skin.name,
                        bones.Length,
                        this.maxBones
                        );
                }
            }
        }

        public void CheckAnimationClip(AssetCheckReport report,AnimationClip clip)
        {
            if (clip.name == "__preview__Take 001")
            {
                return;
            }
            //Unity 具有两种类型的动画：浮点和对象引用。
            //浮点曲线是一种随着时间的推移对浮点属性进行动画化的经典曲线。
            //对象引用“曲线”是一种随着时间的推移对对象引用属性进行动画化的构造
            if (this.maxCurveCountForClip >= 0)
            {
                EditorCurveBinding[] curveBindings = AnimationUtility.GetCurveBindings(clip);
                EditorCurveBinding[] objectReferenceCurveBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
                int num = curveBindings.Length + objectReferenceCurveBindings.Length;
                if (num > this.maxCurveCountForClip)
                {
                    report.ShowWarnInfo("The animation clip <b><color=orange>{0}</color></b> has <b><color=orange>{1}</color></b> curve count, more than {2}.",
                        clip.name,
                        num,
                        this.maxCurveCountForClip
                    );
                }
            }

            if(this.maxKeyFrameCountForClip >= 0)
            {
                EditorCurveBinding[] curveBindings = AnimationUtility.GetCurveBindings(clip);
                EditorCurveBinding[] objectReferenceCurveBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
                int maxCount = 0;
                foreach(var c in curveBindings)
                {
                    var curve = AnimationUtility.GetEditorCurve(clip, c);
                    maxCount += curve.length;
                }
                foreach (var c in objectReferenceCurveBindings)
                {
                    var curve = AnimationUtility.GetObjectReferenceCurve(clip, c);
                    maxCount += curve.Length;
                }

                if (maxCount > this.maxKeyFrameCountForClip)
                {
                    report.ShowWarnInfo("The animation clip <b><color=orange>{0}</color></b> has <b><color=orange>{1}</color></b> key frame count, more than {2}.", 
                        clip.name,
                        maxCount,
                        this.maxKeyFrameCountForClip
                    );
                }

            }


        }

    }
}

